/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bankingsystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Pushpalanka
 */
public class CustomerFrontEnd {

    private String customrID = null;
    private String passwrd = null;
    private Bank bank;
    private BankInterface bi;

    /**
     * Singleton design pattern used to make sure only one bank instance is created.
     * @param bank
     */
    CustomerFrontEnd(Bank bank, BankInterface bi) {
        this.bank = bank;
        this.bi = bi;
        this.login();
    }

    /**
     * Keep the privacy of the customer giving a login using an identical password
     */
    public void login() {
        Hashtable<String, Customer> customerMap = bank.getCustomers();


        //  prompt the user to enter their ID
        System.out.print("Customer ID: ");

        //  open up standard input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));



        //  read the username from the command-line; need to use try/catch with the
        //  readLine() method
        try {
            customrID = br.readLine();
        }
        catch (IOException ioe) {
            System.out.println("IO error trying to read ID!");
            login();
        }

        System.out.print("Password: ");


        try {
            passwrd = br.readLine();
        } catch (IOException ioe) {
            System.out.println("IO error trying to read password!");
            login();
        }


        Customer ccustomer = customerMap.get(customrID);                            //identify considering customer

        try{
        if (ccustomer.checkPassword(passwrd)) {
            System.out.println("You have logged in succesfully.\nYour details are,");
            while (true) {                                                          //continuously running until user exit
                ccustomer.showCustomerDetails();                                    //show personal details of customer
                System.out.println("\n");
                int x = choosePreference();                                         //take user prefered choice of account to look at
                checkAccounts(ccustomer, x);
            }
        }
        else {
            System.out.println("Password mismatch!!!\nPlease try again.");
            login();
            }
        }
        catch (NullPointerException npe) {
        System.err.println("Please enter your ID and Password. ");                  //Handle error if user did not enter details
        login();
        }
    }

    /*
     * Customer decides to view the details of his/her all the accounts at once or just check one
     * prefered account
     */
    public int choosePreference() {
        String x = null;
        int x1 = 0;
        System.out.println("=========Choose type of account you want to check==========");
        System.out.println("1 - Current Account");
        System.out.println("2 - Fixed Account");
        System.out.println("3 - Savings Account");
        System.out.println("4 - Loan");
        System.out.println("5 - All");
        System.out.println("0 - Main menu");
        System.out.println("Your choice:");
        Scanner scanner = new Scanner(System.in);
           x = scanner.nextLine();
        try {
                x1 = Integer.parseInt(x);
            
        }
        catch(NumberFormatException nFE) {                                      //in case user input any non-numeric symbol
            System.out.println("Please Insert a number");


        }
        return x1;

    }

    /**
     * To show details of the accounts and loans of the customer in the bank
     */
    public void checkAccounts(Customer cc, int x) {
        Customer cust = cc;
        int choice = x;
        Hashtable<String, FixedAccount> fixedAccounts = bank.getFixedAccounts();
        Hashtable<String, CurrentAccount> currentAccounts = bank.getCurrentAccounts();
        Hashtable<String, SavingsAccount> savingsAccounts = bank.getSavingsAccounts();
        Hashtable<String, Loan> loan = bank.getLoans();



        /*Allow user to go to the Main Menu for other choices or exit if no need to look at
         *account details
         */
        if (x == 0) {
            try {
                this.bi.init();
                this.finalize();                                                    //allow garbage collector to clean the object
            } catch (Throwable ex) {
                Logger.getLogger(CustomerFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        //Try to read and show details only if there exist an account
        int account = 0;
        if ((cust.getCurrentAccountNo() != null) && (x == 1 || x == 5)) {
            account++;                                                              //modify account int if accounts exist
            currentAccounts.get(cust.getCurrentAccountNo()).show();
        }
        if ((cust.getSavingsAccountNo() != null) && (x == 3 || x == 5)) {
            account++;
            savingsAccounts.get(cust.getSavingsAccountNo()).show();
        }
        if ((cust.getFixedAccountNo() != null) && (x == 2 || x == 5)) {
            account++;
            fixedAccounts.get(cust.getFixedAccountNo()).show();
        }
        if ((cust.getLoanNo() != null) && (x == 4 || x == 5)) {
            account++;
            loan.get(cust.getLoanNo()).showLoanDetails();
        }

        //if no account exist for the customer actively
        if (account == 0) {
            System.out.println("You have no active account in our bank currently.");
        }

    }
}
//end of class
